package fzdy.core.driver.file;

/**
 * Created by Administrator on 2017/3/28 0028.
 */

import fzdy.core.common.SpringWebTool;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

public abstract class BaseFileDrive {
    protected String tempPath = "/temp";

    public BaseFileDrive() {
    }

    protected String getTempPath() {
        String realPath = SpringWebTool.getRealPath(this.tempPath);
        File file = new File(realPath);
        if(!file.exists()) {
            file.mkdirs();
        }

        return realPath;
    }

    protected String uploadTempFile(InputStream in, String name) throws IOException {
        File file = new File(this.getTempPath() + "/" + (new Date()).getTime() + name);
        FileUtils.copyInputStreamToFile(in, file);
        return file.getPath();
    }

    protected String createRandomName(String fileName) {
        String extName = this.getFileExt(fileName);
        byte[] bytes = Base64.encodeBase64(UUID.randomUUID().toString().getBytes());
        String s = new String(bytes);
        return s + extName;
    }

    private String getFileExt(String fileName) {
        int i = fileName.lastIndexOf(".");
        String extName = "";
        if(i >= 0) {
            extName = fileName.substring(i);
        }

        return extName;
    }
}

